package com.xqboss.apps.service.synthesis;

import com.xqboss.apps.domain.synthesis.Synthesis;
import com.xqboss.apps.domain.synthesis.SynthesisWhite;
import com.xqboss.apps.enums.synthesis.SynthesisWhiteTypeEnum;
import com.xqboss.apps.mapper.synthesis.SynthesisWhiteMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.xqboss.chain.domain.chain.dto.BaseRequest;
import java.util.List;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

/**
 * <p>
 * 合成活动次数明细 服务实现类
 * </p>
 *
 * @author 木鱼
 * @since 2023-11-17
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class SynthesisWhiteService extends ServiceImpl<SynthesisWhiteMapper, SynthesisWhite> {
    public List<SynthesisWhite> selectBasicList(BaseRequest request) {
        return baseMapper.selectList(Wrappers.<SynthesisWhite>lambdaQuery());
    }

    public SynthesisWhite selectByIdBasic(Long id) {
        return baseMapper.selectById(id);
    }

    public boolean saveBasic(SynthesisWhite record) {
        return baseMapper.insert(record) > 0;
    }

    public boolean updateBasic(SynthesisWhite record) {
        return baseMapper.updateById(record) > 0;
    }

    public boolean removeBasic(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    /**
     * 获取白名单信息
     *
     * @param userId
     * @param id
     * @return
     */
    public SynthesisWhite getWhiteInfo(Long userId, Long synthesisId, SynthesisWhiteTypeEnum whiteType) {
        return getOne(Wrappers.lambdaQuery(SynthesisWhite.class)
                .eq(SynthesisWhite::getSynthesisId, synthesisId)
                .eq(SynthesisWhite::getUserId, userId)
                .eq(SynthesisWhite::getType, whiteType));
    }

    /**
     * 添加合成成功计数
     * @param userId
     * @param synthesis 合成活动
     */
    public void addSynthesisSuccess(Long userId, Synthesis synthesis, boolean isWhiteUser, int count) {
        boolean update = update(Wrappers.<SynthesisWhite>lambdaUpdate()
                .setSql("now_num = now_num + " + count)
                .eq(SynthesisWhite::getUserId, userId)
                .eq(SynthesisWhite::getSynthesisId, synthesis.getId())
                .eq(SynthesisWhite::getType, SynthesisWhiteTypeEnum.ACTIVITY_LIMIT));
        if(!update){
            SynthesisWhite synthesisWhite = new SynthesisWhite();
            synthesisWhite.setUserId(userId);
            synthesisWhite.setSynthesisId(synthesis.getId());
            synthesisWhite.setType(SynthesisWhiteTypeEnum.ACTIVITY_LIMIT);
            synthesisWhite.setNum(synthesis.getUserMaxNum());
            synthesisWhite.setNowNum(count);
            save(synthesisWhite);
        }

        if(isWhiteUser){
            update(Wrappers.<SynthesisWhite>lambdaUpdate()
                    .setSql("now_num = now_num + " + count)
                    .eq(SynthesisWhite::getUserId, userId)
                    .eq(SynthesisWhite::getSynthesisId, synthesis.getId())
                    .eq(SynthesisWhite::getType, SynthesisWhiteTypeEnum.WHITE_LIMIT));
        }
    }
}